//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.tool.utils;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

public class IAES2 {
    public static String KEY = "itmc_token_users";
    public static String CARDKEY = "itmc_user_card_1";
    public static String TELEPHONE = "itmc_user_teleph";
    public static String USERKEY = "itmc_user_json";
    public static String VERSIONAES = "itmc_version_aes";
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    public IAES2() {
    }

    public static void main(String[] args) {
        for(int i = 0; i < 100; ++i) {
            System.out.println(System.nanoTime() + "===" + System.currentTimeMillis());
        }

    }

    public static String getAesString(String content) throws Exception {
        return aesEncrypt(content, KEY);
    }

    public static String getAesUserString(String content) throws Exception {
        return aesEncrypt(content, USERKEY);
    }

    public static String getAesTelString(String content) throws Exception {
        return aesEncrypt(content, TELEPHONE);
    }

    public static String getAesCardString(String content) throws Exception {
        return aesEncrypt(content, CARDKEY);
    }

    public static String getRealString(String content) throws Exception {
        return aesDecrypt(content, KEY);
    }

    public static String getRealUserString(String content) throws Exception {
        return aesDecrypt(content, USERKEY);
    }

    public static String getRealTelString(String content) throws Exception {
        return aesDecrypt(content, TELEPHONE);
    }

    public static String getRealCardString(String content) throws Exception {
        return aesDecrypt(content, CARDKEY);
    }

    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    public static String aesEncryptTostr(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    private static String base64Encode(byte[] bytes) {
        return Base64Encoder.encode(bytes);
    }

    private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(1, new SecretKeySpec(encryptKey.getBytes(), "AES"));
        return cipher.doFinal(content.getBytes("utf-8"));
    }

    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
    }

    private static byte[] base64Decode(String base64Code) throws Exception {
        return StringUtils.isEmpty(base64Code) ? null : Base64Decoder.decode(base64Code);
    }

    private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(2, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    public static String decrypt(String encryptStr, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(2, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] encryptBytes = Base64Decoder.decode(encryptStr);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }
}
